package com.shop.cereshop.app.tencent.im.callback.impl;

import com.shop.cereshop.app.tencent.im.callback.TimCallbackHandler;
import com.shop.cereshop.commons.tencent.im.model.callback.request.SendMsgExceptionCallbackRequest;
import com.shop.cereshop.commons.tencent.im.model.callback.response.CallbackResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import static com.shop.cereshop.commons.tencent.im.constant.CallbackCommand.GROUP_CALLBACK_SEND_MSG_EXCEPTION;

/**
 * @description: 发送群聊消息异常回处理器,处理 Group.CallbackSendMsgException
 * @author zdd
 * @date 2025/9/11 10:29
 * @version 1.0
 */
@Slf4j
@Service
public class SendMsgExceptionCallbackImpl implements TimCallbackHandler<SendMsgExceptionCallbackRequest> {

    @Override
    public String getCommand() {
        return GROUP_CALLBACK_SEND_MSG_EXCEPTION;
    }

    @Override
    public CallbackResponse handle(SendMsgExceptionCallbackRequest request) {
        log.info("Processing SendMsgException callback: From={}, To={}, MsgKey={}, EventTime={}",
                request.getFromAccount(), request.getToAccount(), request.getMsgKey(), request.getEventTime());
        try {
//            // 记录撤回日志
//            logWithdraw(request);
//
//            // 从您的日志或数据库中删除或标记该消息为已撤回
//            //    注意：MsgKey 是唯一标识，可用于查找之前记录的消息
//            markMessageAsWithdrawn(request);
//
//            // 通知相关系统消息已撤回 (可选)
//            // notifySystemsOfWithdrawal(request);

            // 处理成功
//            response.setActionStatus("OK");
//            response.setErrorCode(0);
//            response.setErrorInfo("");

            log.info("发送群聊消息异常回成功, MsgKey: {}", request.getMsgKey());
            return CallbackResponse.success();

        } catch (Exception e) {
            log.error("发送群聊消息异常回异常, MsgKey: " + request.getMsgKey(), e);
            return CallbackResponse.error("Internal server error: " + e.getMessage());
        }
    }

}
